package fr.uhp.mpr.tools;
import fr.uhp.mpr.bayesnetwork.NetworkTables;
import fr.uhp.mpr.data.DataMeter;
import fr.uhp.mpr.data.Intervalle;




public class BayesRule {

	public static Proba bayesRule(Proba pA, Proba pB, Proba pBA) {
		return new Proba(pBA.getProba() * pA.getProba() / pB.getProba());
	}
	
	public static Proba[] getPressionProba() {
        return Proba.times(
                        NetworkTables.getInstance().getPressureTable(),
                        NetworkTables.getInstance().getTemperatureTable()
                        );
	}
	
	public Proba[] getRainGivenAll(Intervalle h, Intervalle p, Intervalle w) {
		return NetworkTables.getInstance().getRainTable()[DataMeter.findCorrectIntervalle("HUMIDITY", h)]
		                                                 [DataMeter.findCorrectIntervalle("PRESSURE", p)]
		                                                 [DataMeter.findCorrectIntervalle("WIND", w)];
	}
	
	public static Intervalle getTemperatureGivenPressure(Intervalle p) {
		Proba max = new Proba();
		int inter = DataMeter.findCorrectIntervalle("PRESSURE", p);
		Intervalle interMax = null;
		
		for (int i = 0; i < NetworkTables.getInstance().getPressureTable()[0].length; i++) {
			if (NetworkTables.getInstance().getPressureTable()[inter][i].getProba() >= max.getProba()) { 
					max = NetworkTables.getInstance().getPressureTable()[inter][i];
					interMax = DataMeter.getIntervalle("TEMPERATURE")[i];
			}
		}
		
		return interMax;
	}
	
    public static Proba[] getPluieSachant(Intervalle temp){
        int t = DataMeter.findCorrectIntervalle("TEMPERATURE", temp);
        Proba[] r = NetworkTables.getInstance().getRainSachanttemperatureTable()[t];
        System.out.println();
        for(int i = 0;i<r.length;i++)
        	System.out.println("\t"+r[i].getProba());
        return r;
    }
    
    public static Intervalle getIntervallePluieSachant(Intervalle humidity, Intervalle pressure, Intervalle wind) {
            Proba max = new Proba();
            Intervalle maxIntervalle = null;

            int a = DataMeter.findCorrectIntervalle("HUMIDITY", humidity),
            b = DataMeter.findCorrectIntervalle("PRESSURE", pressure),
            c = DataMeter.findCorrectIntervalle("WIND", wind);

            for (int i = 0; i < DataMeter.getIntervalle("RAIN").length; i++) {

                    if (max.getProba() <= NetworkTables.getInstance().getRainTable()[a][b][c][i].getProba()) {
                            max = NetworkTables.getInstance().getRainTable()[a][b][c][i];
                            maxIntervalle = DataMeter.getIntervalle("RAIN")[i];
                            
                    }
            }
            NetworkTables.getInstance().confiance = max.getProba();
            return maxIntervalle;
    }
}
